package cn.uyunsky.platform.service;

import cn.uyunsky.platform.util.sql.SqlPlan;
import cn.uyunsky.platform.util.sql.SqlPlanFactory;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *    不使用Hibernate，采用spring  JdbcTemplate方式执行sql
 * </p>
 */
public abstract class IServiceWithJdbcAbstract extends IServiceAbstract {


	/*spring JdbcTemplate 模板方法 start*/
	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	/*====================jdbcTemplate 方法;处理问号占位符sql 以及 xsql能力===================*/

	public int[] batchUpdate(String sql, List<Object[]> batchArgs) {
		SqlPlan sqlplan = SqlPlanFactory.getInstance().getPlanCache(sql);
		return jdbcTemplate.batchUpdate(sqlplan.getPlanSql(), batchArgs);
	}

	//执行一般DML语句
	public int update(String sql, Map<String,Object> args) {
		SqlPlan sqlplan = SqlPlanFactory.getInstance().getPlanCache(sql);
		return jdbcTemplate.update(sqlplan.getPlanSql(), getArgs(sqlplan, args));
	}

	//查询数值结果
	public int queryForInt(String sql, Map<String,Object> args) {
		SqlPlan sqlplan = SqlPlanFactory.getInstance().getPlanCache(sql);
		try {
			return jdbcTemplate.queryForInt(sqlplan.getPlanSql(), getArgs(sqlplan, args));
		} catch (IncorrectResultSizeDataAccessException e) {
			if (e.getActualSize() == 0) {
				return 0;
			} else {
				throw e;
			}
		}
	}

	//查询长数值结果
	public long queryForLong(String sql, Map args) {
		SqlPlan sqlplan = SqlPlanFactory.getInstance().getPlanCache(sql);
		try {
			return jdbcTemplate.queryForLong(sqlplan.getPlanSql(), getArgs(sqlplan, args));
		} catch (IncorrectResultSizeDataAccessException e) {
			if (e.getActualSize() == 0) {
				return 0;
			} else {
				throw e;
			}
		}
	}

	//查询单一Map结果
	public Map queryForMap(String sql, Map args) {
		SqlPlan sqlplan = SqlPlanFactory.getInstance().getPlanCache(sql);
		try {
			return jdbcTemplate.queryForMap(sqlplan.getPlanSql(), getArgs(sqlplan, args));
		} catch (IncorrectResultSizeDataAccessException e) {
			if (e.getActualSize() == 0) {
				return null;
			} else {
				throw e;
			}
		}
	}

	//查询单一class对象结果
	public Object queryForObject(String sql, Class elementType, Map args) {
		SqlPlan sqlplan = SqlPlanFactory.getInstance().getPlanCache(sql);
		try {
			return jdbcTemplate.queryForObject(sqlplan.getPlanSql(), getArgs(sqlplan, args), elementType);
		} catch (IncorrectResultSizeDataAccessException e) {
			if (e.getActualSize() == 0) {
				return null;
			} else {
				throw e;
			}
		}
	}

	//查询结果为List<Map>
	public List queryForList(String sql, Map args) {
		SqlPlan sqlplan = SqlPlanFactory.getInstance().getPlanCache(sql);
		try {
			return jdbcTemplate.queryForList(sqlplan.getPlanSql(), getArgs(sqlplan, args));
		} catch (IncorrectResultSizeDataAccessException e) {
			if (e.getActualSize() == 0) {
				return null;
			} else {
				throw e;
			}
		}
	}

	//查询class对象数值
	public List queryForList(String sql, Class elementType, Map args) {
		SqlPlan sqlplan = SqlPlanFactory.getInstance().getPlanCache(sql);
		try {
			return jdbcTemplate.queryForList(sqlplan.getPlanSql(), getArgs(sqlplan, args), elementType);
		} catch (IncorrectResultSizeDataAccessException e) {
			if (e.getActualSize() == 0) {
				return null;
			} else {
				throw e;
			}
		}
	}

    //TODO xsql

}
